<template>
<div>
    <div class="secretary mtb-10">
        <div class="secretary-box">
            <div class="box-title">名称:</div>
            <el-input v-model="LeadingList[0].tip" :disabled="disabled"></el-input>
        </div>
        <el-row>
            <el-button type="edit" @click="disabled=false" :disabled="!disabled">编辑</el-button>
            <el-button type="preservation" @click="disabled=true" :disabled="disabled">保存</el-button>
        </el-row>
    </div>
    <!--渲染列表区域-->
    <div class="secretary mtb-10 height-190" v-for="(item,index) in LeadingList.slice(1)" :key="index">
        <div class="secretary-box">
            <div class="box-title">岗位名称:</div>
            <el-input v-model="item.tip" placeholder="请输入内容" :disabled="index !== editIndex"></el-input>
        </div>
        <div class="secretary-box">
            <div class="box-title">人员姓名:</div>
            <el-input v-model="item.content" placeholder="请输入内容" :disabled="index !== editIndex"></el-input>
        </div>
        <el-row>
            <el-button type="edit" @click="setIndex(index)" :disabled="index === editIndex">编辑</el-button>
            <el-button type="preservation" @click="SaveChange(item.id,item.tip,item.content)" :disabled="index !== editIndex">保存</el-button>
            <Single-deletion :clearOne="clearOne" :id="item.id"></Single-deletion>
            <el-button type="newlyBuild" @click="BuildNew(index)">新建</el-button>
        </el-row>
    </div>
</div>
</template>

<script>
import SingleDeletion from "@/components/Popup/SingleDeletion";

export default {
    components: {
        SingleDeletion,
    },
    data() {
        return {
            LeadingList: [{
                tip: ""
            }],
            //控制输入框是否可编辑
            editIndex: -1,
            disabled: true,
        };
    },
    created() {
        this.GetLeadingList();
    },
    methods: {
        //廉政档案接口获取列表-领导机构
        GetLeadingList() {
            this.Api.getDanganLeadingList().then((res) => {
                if (res.code === "0") {
                    this.LeadingList = res.data;
                    // console.log(this.LeadingList, 11111);
                }
            });
        },
        //修改编辑
        setIndex(index) {
            this.editIndex = index;
        },
        //修改，并保存
        SaveChange(id, tip, content) {
            // console.log(id);
            if (id !== "") {
                this.Api.getDanganLeadingChange({
                    content: content,
                    id: id,
                    tip: tip,
                }).then((res) => {
                    // console.log(res, 111);
                    if (res.code === "0") {
                        // console.log(res, 111222);
                        this.editIndex = -1;
                    }
                });
            } else {
                this.Api.getDanganLeadingAdd({
                    content: content,
                    tip: tip,
                }).then((res) => {
                    if (res.code === "0") {
                        // console.log("成功");
                        this.editIndex = -1;
                    }
                });
            }
        },
        //删除的接口
        clearOne(id) {
            this.Api.getDanganLeadingClear({
                id: id
            }).then((res) => {
                // console.log(res,111)
                this.GetLeadingList();
            });
        },
        //新建一个新的
        BuildNew(index) {
            this.editIndex = index + 1;
            this.LeadingList.splice(index + 2, 0, {
                id: "",
                type: "",
                tip: "",
                content: "",
                sort: "",
                deleted: "",
            });
            // console.log(this.LeadingList, 1212);
        },
    },
};
</script>

<style lang="less" scoped>
.secretary {
    height: 134px;
    background: rgba(248, 248, 249, 1);
    padding: 30px 0 0 40px;

    .secretary-box {
        display: flex;
        align-items: center;
        margin-bottom: 20px;

        .box-title {
            width: 75px;
            font-size: 14px;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: rgba(51, 51, 51, 1);
        }

        /deep/.el-input__inner {
            width: 360px;
            height: 36px;
            border-radius: 2px;
            border: 1px solid rgba(221, 221, 221, 1);
        }
    }

    /deep/.el-row {
        margin-left: 70px;
        display: flex;

        .el-button--edit {
            width: 60px;
            height: 28px;
            background: rgba(255, 255, 255, 1);
            border-radius: 4px;
            border: 1px solid rgba(221, 221, 221, 1);
            padding: 0;
            color: #333333;
            margin-right: 11px;
        }

        .el-button--preservation {
            width: 60px;
            height: 28px;
            background: rgba(255, 255, 255, 1);
            border-radius: 4px;
            border: 1px solid rgba(221, 221, 221, 1);
            padding: 0;
            color: #333333;
            margin-right: 11px;
        }
    }
}

.mtb-10 {
    margin: 10px 0 20px 0;
}

.mb-20 {
    margin-bottom: 20px;
}

.height-190 {
    height: 190px;
}

/deep/.el-button--delete {
    width: 60px;
    height: 28px;
    background: rgba(251, 54, 63, 1) !important;
    border-radius: 4px;
    padding: 0;
    color: #ffffff !important;
}

/deep/.el-button--newlyBuild {
    width: 60px;
    height: 28px;
    background: rgba(61, 127, 255, 1) !important;
    border-radius: 4px;
    padding: 0;
    color: #ffffff !important;
    margin-left: 10px;
}

/deep/.el-input__inner {
    color: #333333 !important;
}

.el-row {
    .is-disabled {
        background-color: #F8F8F9 !important;
    }
}
</style>
